package com.cmict.data.bpm.service.externalfile;

import cn.hutool.core.collection.CollectionUtil;
import com.cmict.data.bpm.controller.admin.externalfile.vo.ExternalFileCreateReqVO;
import com.cmict.data.bpm.controller.admin.externalfile.vo.ExternalFileExportReqVO;
import com.cmict.data.bpm.controller.admin.externalfile.vo.ExternalFilePageReqVO;
import com.cmict.data.bpm.controller.admin.externalfile.vo.ExternalFileUpdateReqVO;
import com.cmict.data.bpm.convert.externalfile.ExternalFileConvert;
import com.cmict.data.bpm.dal.dataobject.externalfile.ExternalFileDO;
import com.cmict.data.bpm.dal.mysql.externalfile.ExternalFileMapper;
import com.cmict.data.bpm.enums.externalfile.ExternalFileTypeEnum;
import com.cmict.data.framework.common.pojo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.cmict.data.bpm.enums.ErrorCodeConstants.EXTERNAL_FILE_NOT_EXISTS;
import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 工作流审批附件 Service 实现类
 */
@Service
@Validated
public class ExternalFileServiceImpl implements ExternalFileService {

    @Resource
    private ExternalFileMapper externalFileMapper;

    @Override
    public Long createExternalFile(ExternalFileCreateReqVO createReqVO) {
        // 插入
        ExternalFileDO externalFile = ExternalFileConvert.INSTANCE.convert(createReqVO);
        externalFileMapper.insert(externalFile);
        // 返回
        return externalFile.getId();
    }

    @Override
    public void updateExternalFile(ExternalFileUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateExternalFileExists(updateReqVO.getId());
        // 更新
        ExternalFileDO updateObj = ExternalFileConvert.INSTANCE.convert(updateReqVO);
        externalFileMapper.updateById(updateObj);
    }

    @Override
    public void deleteExternalFile(Long id) {
        // 校验存在
        this.validateExternalFileExists(id);
        // 删除
        externalFileMapper.deleteById(id);
    }

    @Override
    public void createExternalFile(ExternalFileCreateBO fileCreateBO) {
        List<ExternalFileInfo> fileList = fileCreateBO.getFileList();
        if (CollectionUtil.isNotEmpty(fileList)) {
            fileList.stream().map(item -> {
                ExternalFileCreateReqVO fileVO = createFileDaoInfo(item, ExternalFileTypeEnum.FILE, fileCreateBO);
                return createExternalFile(fileVO);
            }).collect(Collectors.toList());
        }
        List<ExternalFileInfo> imageList = fileCreateBO.getImageList();
        if (CollectionUtil.isNotEmpty(imageList))  imageList.stream().map(item -> {
            ExternalFileCreateReqVO fileVO = createFileDaoInfo(item, ExternalFileTypeEnum.IMAGE, fileCreateBO);
            return createExternalFile(fileVO);
        }).collect(Collectors.toList());
    }

    private ExternalFileCreateReqVO createFileDaoInfo (ExternalFileInfo item,ExternalFileTypeEnum type, ExternalFileCreateBO fileCreateBO) {
        ExternalFileCreateReqVO fileVO = new ExternalFileCreateReqVO();
        fileVO.setName(item.getName());
        fileVO.setUrl(item.getUrl());
        fileVO.setType(type.getType());
        fileVO.setProcessInstanceId(fileCreateBO.getProcessInstanceId());
        fileVO.setTaskId(fileCreateBO.getTaskId());
        return fileVO;
    }
    private void validateExternalFileExists(Long id) {
        if (externalFileMapper.selectById(id) == null) {
            throw exception(EXTERNAL_FILE_NOT_EXISTS);
        }
    }

    @Override
    public ExternalFileDO getExternalFile(Long id) {
        return externalFileMapper.selectById(id);
    }

    @Override
    public List<ExternalFileDO> getExternalFileList(Collection<Long> ids) {
        return externalFileMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ExternalFileDO> getExternalFilePage(ExternalFilePageReqVO pageReqVO) {
        return externalFileMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ExternalFileDO> getExternalFileList(ExternalFileExportReqVO exportReqVO) {
        return externalFileMapper.selectList(exportReqVO);
    }

    @Override
    public List<ExternalFileDO> getExternalFileList(String processInstanceId) {
        ExternalFileExportReqVO fileExportReqVO = new ExternalFileExportReqVO().setProcessInstanceId(processInstanceId);
        return getExternalFileList(fileExportReqVO);
    }
}
